const {gt, gte, lt, lte, eq, get} = require('./lib')

function not(v1, v2) {
    return v1 != v2
}

function notAndType(v1, v2) {
    return v1 !== v2
}

function or(v1, v2) {
    return v1 || v2
}


let funMap = {
    gt,
    gte,
    lt,
    lte,
    eq,
    or,
    not
}

function con(item, index, key, funName, ...args) {
    let fun = funMap[funName];
    return fun(get(item, key), ...args);
}
exports.con = con;


/**
 * 
 * @param items {[]}
 * @param conditions  {[]}
 * @returns 
 */
function findAll(items, conditions = []) {
    return items.filter(function (item, index) {
        return conditions.every((condition) => {
            return con(item, index, ...condition.slice(0));
        });
    });
}
exports.findAll = findAll


exports.toggleArray = function(arr, item) {
    var index = arr.indexOf(item);

    if (index < 0) {
        arr.push(item);
    } else {
        arr.splice(index, 1);
    }
}